--
-- Trigger para asegurarse que no se modifique el importe de un valor que tiene un movimiento (posterior o anterior)
-- 

CREATE OR REPLACE FUNCTION tg_ctem_val_upd_impomoneda()
  RETURNS trigger AS
$BODY$
BEGIN
  IF NEW.IdVal IS NOT NULL THEN 
     IF EXISTS (SELECT * FROM CTEM WHERE IdVal = New.IdVal AND Ctem.ImpoMoneda <> NEW.ImpoMoneda) THEN 
        RAISE EXCEPTION 'Ha modificado el importe de un valor con otros movimientos ... este caso es inaceptable.';
     END IF;
   END IF;
RETURN NEW;
END;$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION tg_ctem_val_upd_impomoneda()
  OWNER TO abacouser;

CREATE TRIGGER ctem_upd_val_impomoneda
  AFTER UPDATE OF IMPOMONEDA
  ON ctem
  FOR EACH ROW
  EXECUTE PROCEDURE tg_ctem_val_upd_impomoneda();

--=======================================
SELECT aaVersion('444.00','443.00');
--=======================================
